package com.klun.hdfs.hd.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsClient {
    /**
     * hdfs 查看文件详情
     *
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testListFiles() throws URISyntaxException, IOException, InterruptedException {

    }


        /**
         * hdfs 文件夹删除
         *
         * @throws URISyntaxException
         * @throws IOException
         * @throws InterruptedException
         */
    @Test
    public void testDelete() throws URISyntaxException, IOException, InterruptedException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), configuration, "atguigu");

        //2 执行删除
        fs.delete(new Path("/1108"),true);

        fs.close();
    }


    /**
     * hdfs 文件下载
     *
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), configuration, "atguigu");

        //2 执行下载
        fs.copyToLocalFile(false,new Path("/hello.txt"),new Path("f:/hello.txt"),true);

        fs.close();
    }

    // 参数优先级
    @Test
    public void testCopyFromLocalFile() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "1");
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), configuration, "atguigu");
        fs.copyFromLocalFile(new Path("D:\\myInstall\\hadoop-2.7.2\\a.txt"), new Path("/hello.txt"));
        fs.close();
        System.out.println("over");
    }

    @Test
    public void testMkdirs() throws URISyntaxException, IOException, InterruptedException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // 配置在集群上运行
        //configuration.set("fa.defaultFS","hdfs://init1:9000");
        //FileSystem fs = FileSystem.get(configuration);
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), configuration, "atguigu");
        // 2 创建目录
        fs.mkdirs(new Path("/1108/daxian/banzhang"));
        // 3 关闭资源
        fs.close();
    }

}
